
from django.db import models
from order_app.models import Order
from vehicle_app.models import Vehicle


class LocationRecord(models.Model):
    """位置记录模型"""
    
    vehicle = models.ForeignKey(Vehicle, on_delete=models.CASCADE, related_name='location_records', verbose_name='车辆')
    order = models.ForeignKey(Order, on_delete=models.CASCADE, related_name='location_records', null=True, blank=True, verbose_name='订单')
    
    # 位置信息
    longitude = models.DecimalField(max_digits=9, decimal_places=6, verbose_name='经度')
    latitude = models.DecimalField(max_digits=9, decimal_places=6, verbose_name='纬度')
    altitude = models.DecimalField(max_digits=9, decimal_places=2, null=True, blank=True, verbose_name='海拔(米)')
    accuracy = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True, verbose_name='精度(米)')
    speed = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True, verbose_name='速度(km/h)')
    direction = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True, verbose_name='方向(度)')
    
    # 地址信息
    address = models.CharField(max_length=255, null=True, blank=True, verbose_name='地址')
    province = models.CharField(max_length=50, null=True, blank=True, verbose_name='省份')
    city = models.CharField(max_length=50, null=True, blank=True, verbose_name='城市')
    district = models.CharField(max_length=50, null=True, blank=True, verbose_name='区县')
    
    # 时间戳
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    
    class Meta:
        verbose_name = '位置记录'
        verbose_name_plural = '位置记录'
        ordering = ['-created_at']
        indexes = [
            models.Index(fields=['vehicle', 'created_at']),
            models.Index(fields=['order', 'created_at']),
        ]
    
    def __str__(self):
        return f"{self.vehicle.license_plate} 在 {self.created_at} 的位置"


class Milestone(models.Model):
    """里程碑模型，记录订单运输过程中的重要节点"""
    
    # 里程碑类型选项
    class Type(models.TextChoices):
        START = 'start', '开始运输'
        ARRIVE_LOADING = 'arrive_loading', '到达装货点'
        LOADING_COMPLETE = 'loading_complete', '装货完成'
        ARRIVE_DELIVERY = 'arrive_delivery', '到达卸货点'
        DELIVERY_COMPLETE = 'delivery_complete', '卸货完成'
        COMPLETE = 'complete', '运输完成'
        CUSTOM = 'custom', '自定义'
    
    order = models.ForeignKey(Order, on_delete=models.CASCADE, related_name='milestones', verbose_name='订单')
    milestone_type = models.CharField(max_length=20, choices=Type.choices, verbose_name='里程碑类型')
    description = models.CharField(max_length=100, null=True, blank=True, verbose_name='描述')
    
    # 位置信息
    longitude = models.DecimalField(max_digits=9, decimal_places=6, null=True, blank=True, verbose_name='经度')
    latitude = models.DecimalField(max_digits=9, decimal_places=6, null=True, blank=True, verbose_name='纬度')
    address = models.CharField(max_length=255, null=True, blank=True, verbose_name='地址')
    
    # 时间戳
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    
    class Meta:
        verbose_name = '里程碑'
        verbose_name_plural = '里程碑'
        ordering = ['order', 'created_at']
    
    def __str__(self):
        return f"订单 {self.order.order_number} 的 {self.get_milestone_type_display()}"


class TrackingSettings(models.Model):
    """跟踪设置模型，用于配置车辆位置上报频率等参数"""
    
    vehicle = models.OneToOneField(Vehicle, on_delete=models.CASCADE, related_name='tracking_settings', verbose_name='车辆')
    
    # 上报频率设置
    report_interval = models.IntegerField(default=60, verbose_name='上报间隔(秒)')
    report_distance = models.IntegerField(default=100, verbose_name='上报距离(米)')
    
    # 省电模式设置
    power_saving_mode = models.BooleanField(default=False, verbose_name='省电模式')
    power_saving_interval = models.IntegerField(default=300, verbose_name='省电模式上报间隔(秒)')
    
    # 是否启用跟踪
    is_tracking_enabled = models.BooleanField(default=True, verbose_name='是否启用跟踪')
    
    # 时间戳
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        verbose_name = '跟踪设置'
        verbose_name_plural = '跟踪设置'
    
    def __str__(self):
        return f"{self.vehicle.license_plate} 的跟踪设置"